Passed
Branch master (74b6f9)
by Stephan
02:33
created

ext.SimpleBatchUpload.js ➔ ... ➔ data.success   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 2
nc 2
nop 1
1
/**
2
 * File containing the SimpleBatchUpload class
3
 *
4
 * @copyright (C) 2016 - 2017, Stephan Gambke
5
 * @license   GNU General Public License, version 2 (or any later version)
6
 *
7
 * This software is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 * This software is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * @file
19
 * @ingroup SimpleBatchUpload
20
 */
21
22
/** global: mediaWiki */
23
/** global: jQuery */
24
25
;( function ( $, mw, undefined ) {
26
27
	'use strict';
28
29
	$( function () {
30
		$( 'span.fileupload-container' ).each( function () {
31
32
			var container = this;
33
34
			$( 'input.fileupload', container )
35
36
			.on( 'change drop', function ( /* e, data */ ) { $( 'ul.fileupload-results', container ).empty(); } )
37
38
			.fileupload( {
39
				dataType: 'json',
40
				dropZone: $( '.fileupload-dropzone', container ),
41
				progressInterval: 100,
42
43
44
				add: function ( e, data ) {
45
46
					var that = this;
47
					data.id = Date.now();
48
49
					var status = $( '<li>' )
50
					.attr( 'id', data.id )
51
					.text( data.files[ 0 ].name );
52
53
					$( 'ul.fileupload-results', container ).append( status );
54
55
					var api = new mw.Api();
56
57
					var tokenType = 'csrf';
58
59
					if ( mw.config.get( 'wgVersion' ) < '1.27.0' ) {
60
						tokenType = 'edit';
61
					}
62
63
					// invalidate cached token; always request a new one
64
					api.badToken( tokenType );
65
66
					api.getToken( tokenType )
67
					.then(
68
						function ( token ) {
69
70
							data.formData = {
71
								format: 'json',
72
								action: 'upload',
73
								token: token,
74
								ignorewarnings: 1,
75
								text: $( that ).fileupload( 'option', 'text' ),
76
								comment: $( that ).fileupload( 'option', 'comment' ),
77
								filename: data.files[ 0 ].name
78
							};
79
80
							data.submit()
81
							.success( function ( result /*, textStatus, jqXHR */ ) {
82
83
								if ( result.error !== undefined ) {
84
85
									status.text( status.text() + " ERROR: " + result.error.info ).addClass( 'ful-error api-error' );
86
87
								} else {
88
									var link = $( '<a>' );
89
									link
90
									.attr( 'href', mw.Title.newFromFileName( result.upload.filename ).getUrl() )
91
									.text( result.upload.filename );
92
93
									status
94
									.addClass( 'ful-success' )
95
									.text( ' OK' )
96
									.prepend( link );
97
								}
98
99
							} )
100
							.error( function ( /* jqXHR, textStatus, errorThrown */ ) {
101
								status.text( status.text() + " ERROR: Server communication failed." ).addClass( 'ful-error server-error' );
102
								// console.log( JSON.stringify( arguments ) );
103
							} );
104
						},
105
						function () {
106
							status.text( status.text() + " ERROR: Could not get token." ).addClass( 'ful-error token-error' );
107
							// console.log( JSON.stringify( arguments ) );
108
						}
109
					);
110
111
				},
112
113
				progress: function ( e, data ) {
114
					if ( data.loaded !== data.total ) {
115
						$( '#' + data.id )
116
						.text( data.files[ 0 ].name + ' ' + parseInt( data.loaded / data.total * 100, 10 ) + '%' );
117
					}
118
				}
119
			} );
120
		} );
121
122
		$( document ).bind( 'drop dragover', function ( e ) {
123
			e.preventDefault();
124
		} );
125
	} );
126
127
}( jQuery, mediaWiki ));
128